var app = getApp()
var pageObject = {
  data: {
    lists: [],
    hiddenmodalput:true,
    keyword:'',
    product: {
      id:'',
      nums:1,
      price:'',
      name:'',
      factory:'',  
    },

  },
  onLoad: function() {
    this.getProductList()
  },
  cancel: function(){  
    this.setData({  
        hiddenmodalput: true,
        'product.nums' : 1
    });  
  },  
  inputNums: function(e){ 
    this.setData({  
      'product.nums' : e.detail.value  
     });  
  },
  inputKeyword: function(e){
    this.setData({
      'keyword': e.detail.value
    })
  },
  //确认  
  confirm: function(event){  
    var _this = this; 
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    wx.request({ 
      url: app.globalData.buy_add_url,
      data: {userId:loginUserId,productId:_this.data.product.id,nums:_this.data.product.nums,finish:0,price:_this.data.product.price,name:_this.data.product.name,factory:_this.data.product.factory},
      method: 'POST', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno == 0){
           _this.setData({hiddenmodalput: true});  
           app.showModel("添加购物车结果",resultData.message)

        }
      },
      fail: function () {
        setTimeout(function () {
          wx.showToast({
            title: "加载失败"
          })
        }, 100)
      }
    })
  } ,
  onPullDownRefresh: function() {
    this.getProductList()
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 1000
    })
  },
  getProductList: function() {
    var _this = this; 
    wx.request({ 
      url: app.globalData.product_list_url,
      data: {keyword:_this.data.keyword},
      method: 'GET', 
      success: function (res) {
        var resultData = res.data;
        if(resultData.errno==0){
          _this.setData({lists: resultData.data})  
        }
      },
      fail: function () {
        setTimeout(function () {
          wx.showToast({
            title: "加载失败"
          })
        }, 100)
      }
    }) 
  },
  gotoDetail: function(event) { 
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../product-detail/product-detail?id=' + id
    })
  },
  buy: function(event) {     
    var loginUserId = wx.getStorageSync('LOGINUSER_ID') || '';
    if(loginUserId == ''){
      wx.redirectTo({
        url: '../index/index?redirect=product-list/product-list'
      })
    }else{    
        this.setData({  
          hiddenmodalput: false,
          'product.id':event.currentTarget.dataset.id,
          'product.nums':1,
          'product.price':event.currentTarget.dataset.price,
          'product.factory':event.currentTarget.dataset.factory,
          'product.name':event.currentTarget.dataset.name
      })  
    }
  }
}

Page(pageObject)
